package com.situ.springboot.controller;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.situ.springboot.pojo.User;
import com.situ.springboot.service.IUserService;
import com.situ.springboot.util.JSONResult;
import com.situ.springboot.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    // IUserService userService = new UserServiceImpl();
    @Autowired
    private IUserService userService;

    @RequestMapping("/getLoginPage")
    public String getLoginPage() {
        return "/WEB-INF/login.jsp";
    }

    @RequestMapping("/login")
    @ResponseBody
    public JSONResult login(String name, String password, String code, Integer type, HttpSession session) {
        // 首先验证用户输入的验证码对不对，
        // 只有验证码正确，再去验证用户名和密码是否正确
        // session.setAttribute("authCode", strCode.toLowerCase());
        String authCode = (String) session.getAttribute("authCode");
        // 用户输入验证码为空或者后台生成的验证码和用户输入的不相等
        if (StringUtils.isEmpty(code) || !authCode.equalsIgnoreCase(code)) {
            return JSONResult.error("验证码为空或者错误");
        }

        User user = userService.login(name, password, type);
        if (user == null) {
            return JSONResult.error("没有该用户名密码的用户");
        } else {
            // 把用户登录的信息放到Session，作为登录凭证
            // Session是服务器端为每个用户开辟的一块内存区域
            session.setAttribute("user", user);
            return JSONResult.ok("登录成功");
        }

    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        // 销毁Session
        session.invalidate();
        // 跳转到登录界面
        return "redirect:/user/getLoginPage";
    }




    // http://localhost:8080/user/getUserListPage
    @RequestMapping("/getUserListPage")
    public String getUserListPage(Model model) { // 返回值String代表要转发的界面
        // List<User> list  = userService.selectAll();
        // model.addAttribute("list", list);
        return "/WEB-INF/user_list.jsp";

    }

    // /user/deleteById?id=1
    @RequestMapping("/deleteById")
    @ResponseBody
    public JSONResult deleteById(Integer id) {
        // sout
        System.out.println("id: " + id);
        // Alt+Enter
        userService.deleteById(id);

        // 删除完了之后应该，再去完成一次查找所有展示数据的过程
        // return "redirect:/user/getUserListPage";

        return JSONResult.ok("删除成功");
    }

    // http://localhost:8080/user/selectByPage?page=1&limit=10&id=9&name=zs&age=23
    // page代表：第几页数据   limit:每页有多少条数据
    @RequestMapping("/selectByPage")
    @ResponseBody
    public LayUITableResult selectByPage(Integer page, Integer limit, User user) {
        LayUITableResult layUITableResult = userService.selectByPage(page, limit, user);
        return layUITableResult;
    }

    @RequestMapping("/getUserAddPage")
    public String getUserAddPage() {
        return "/WEB-INF/user_add.jsp";
    }

    @RequestMapping("/add")
    @ResponseBody
    public JSONResult add(User user) {
        userService.add(user);
        return JSONResult.ok("添加成功");
    }

    @RequestMapping("/getUserUpdatePage")
    public String getUserUpdatePage(Integer id, Model model) {
        /*根据id查出要更新的学生信息*/
        User user = userService.selectById(id);
        // 把查出来的数据放到Model内存里面，然后转发到user_update.jsp页面进行展示
        model.addAttribute("user", user);
        return "/WEB-INF/user_update.jsp";
    }

    @RequestMapping("/update")
    @ResponseBody
    public JSONResult update(User user) {
        userService.update(user);
        return JSONResult.ok("更新成功");
    }

    // '10,16'
    @RequestMapping("/deleteAll")
    @ResponseBody
    public JSONResult deleteAll(Integer[] ids) {
        userService.deleteAll(ids);
        return JSONResult.ok("删除成功");
    }
}
